//
//  DashboardViewController.m
//  APJ HRadar
//
//  Created by Richard Zhao on 3/4/14.
//  Copyright (c) 2014 APJ. A	ll rights reserved.
//A

#import "DashboardViewController.h"
#import "LoginViewController.h"
#import "syncDataService.h"
#import "Staff.h"
#import "webService.h"
#import "SimpleBarChart.h"
#import "HRStore.h"
#import "ResourceRequirement.h"
#import "Dept.h"
#import "RequirementDetail.h"

@interface DashboardViewController ()

@end

@implementation DashboardViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UITabBarItem *tab = [[UITabBarItem alloc] initWithTitle:@"Requirement" image:[UIImage imageNamed:@"tab_dashboard"] tag:1];
        self.tabBarItem = tab;
                self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]];
        //self.view.backgroundColor = [UIColor clearColor];
     }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [[self navigationItem] setTitle:@"HR Requirement Sheet"];
    [self loadLegend];
}
- (void)loadLegend
{
    [self loadContentView];
    [self loadDeptview];
    [self loadSkillviewInit];
    [_skillview.view setHidden:YES];
    [self loadUISegmentedControl];
}

-(void) loadContentView
{
    CGRect contentFrame;
    contentFrame.size.width = 1000;
    contentFrame.size.height = 580;
    contentFrame.origin.x = 10;
    contentFrame.origin.y = 115;
    self->contentView = [[UIView alloc] initWithFrame:contentFrame];
    [self.view addSubview:contentView];
}

-(void) loadUISegmentedControl
{
    self->segmentControl = [[UISegmentedControl alloc] init];
    CGRect segmentFrame;
    segmentFrame.size.width = 500;
    segmentFrame.size.height = 30;
    segmentFrame.origin.x = 260;
    segmentFrame.origin.y = 80;
    [self->segmentControl setFrame:segmentFrame];
    [self->segmentControl setTintColor:[UIColor colorWithRed:100/255.0 green:100/255.0 blue:200/255.0 alpha:0.9]];
    [self->segmentControl insertSegmentWithTitle:@"By Department" atIndex:0 animated:YES];
    [self->segmentControl insertSegmentWithTitle:@"By Skills" atIndex:1 animated:YES];
    [self->segmentControl addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
    [self->segmentControl setSelectedSegmentIndex:0];
    [self.view addSubview:self->segmentControl];
    segmentControl.backgroundColor =[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:0.5];
}

-(void) segmentClick:(id)sender
{
    [self hideAll];
    switch ([sender selectedSegmentIndex]) {
        case 0:
            [self loadDeptview];
            break;
        case 1:
            [self loadSkillview];
            break;
        default:
            NSLog(@"这句话绝对不会出现！如果出现了，那么就证明segment在点击时出错了！看到后一定要排查一下！");
            break;
    }
}

- (void)hideAll
{
    [_deptview.view setHidden:YES];
    [_skillview.view setHidden:YES];
}

- (void)loadDeptview
{
    //[self->contentView setBackgroundColor:[UIColor whiteColor]]; // test
    if (nil == _deptview) {
        _deptview = [[DashboardDeptController alloc] init];
        [self.view addSubview:[_deptview getDeptView]];
    }
    else{
        [_deptview bindDateForChart];
    }
    
    [_deptview sentNavigationController:self.navigationController];
    [_deptview.view setHidden:NO];
}

- (void)loadSkillview
{
    //[self->contentView setBackgroundColor:[UIColor greenColor]]; // test
    if (nil == _skillview) {
        _skillview = [[DashboardSkillController alloc] init];
        [self.view addSubview:[_skillview getSkillView]];
    }else{
    [_skillview bindDateForChart];
    }
    [_skillview sentNavigationController:self.navigationController];
    [_skillview.view setHidden:NO];
}

- (void)loadSkillviewInit
{
    //[self->contentView setBackgroundColor:[UIColor greenColor]]; // test
    if (nil == _skillview) {
        _skillview = [[DashboardSkillController alloc] init];
        [self.view addSubview:[_skillview getSkillView]];
    }
    [_skillview sentNavigationController:self.navigationController];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    extern BOOL hasLogin;
    if (!hasLogin) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [self presentViewController:login animated:YES completion:nil];
    } else {
        [_skillview sentNavigationController:self.navigationController];
        [_deptview sentNavigationController:self.navigationController];
        [self hideAll];
        switch ([self->segmentControl selectedSegmentIndex]) {
            case 0:
                [self loadDeptview];
                break;
            case 1:
                [self loadSkillview];
                break;
            default:
                NSLog(@"这句话绝对不会出现！如果出现了，那么就证明segment在点击时出错了！看到后一定要排查一下！");
                break;
        }

    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
